do STARdata

*choice of outcome of interest
gen Y = math


*controls: school ID by free lunch status fixed effects
xi i.poor*i.schid
local X = "_I*" 
keep Y `X' D Z poor

*group towards which we might relocate ressources
local targetgroup="poor"

*possible policydifferences under linear budget constraint
egen mean`targetgroup'= mean(`targetgroup')
gen hab`targetgroup' = (`targetgroup' - mean`targetgroup')/(1-mean`targetgroup')

*running index for table entries
scalar trow=1

 

*run analysis 
foreach tau in 0.2 0.3 0.4 0.5 0.6 0.7 0.8{		
	local tablerow = trow
	local labl`tablerow'= "`tau'th quantile "
	quietly distIVbounds Y `X', d(D) z(Z) quantile(`tau')

	quietly sum gphilower if `targetgroup'==1
	scalar col1`tablerow'=r(mean)
	quietly sum gphiupper if `targetgroup'==1
	scalar col2`tablerow'=r(mean)
	quietly sum gphilower if `targetgroup'==0
	scalar col3`tablerow'=r(mean)
	quietly sum gphiupper if `targetgroup'==0
	scalar col4`tablerow'=r(mean)
	scalar col4`tablerow'=r(mean)
	quietly sum gphilower 
	scalar col5`tablerow'=r(mean)
	quietly sum gphiupper
	scalar col6`tablerow'=r(mean)
	quietly policyeffect hab`targetgroup' gphiupper gphilower
	scalar col7`tablerow'= r(phiablower)
	scalar col8`tablerow'= r(phiabupper)
	scalar trow=trow+1
	drop IF* gphi* w
}

 

foreach tau in 0.2 0.3 0.4 0.5 0.6 0.7 0.8{	
	local tablerow = trow
	local labl`tablerow'= "`tau'th quantile "	
	quietly distIVboundsmonotone Y `X', d(D) z(Z) quantile(`tau')
	quietly sum gphilower if `targetgroup'==1
	scalar col1`tablerow'=r(mean)
	quietly sum gphiupper if `targetgroup'==1
	scalar col2`tablerow'=r(mean)
	quietly sum gphilower if `targetgroup'==0
	scalar col3`tablerow'=r(mean)
	quietly sum gphiupper if `targetgroup'==0
	scalar col4`tablerow'=r(mean)
	quietly sum gphilower 
	scalar col5`tablerow'=r(mean)
	quietly sum gphiupper
	scalar col6`tablerow'=r(mean)
	quietly policyeffect hab`targetgroup' gphiupper gphilower
	scalar col7`tablerow'= r(phiablower)
	scalar col8`tablerow'= r(phiabupper)
	scalar trow=trow+1
	drop IF* gphi* w
}



*produce output that can be copied to LATEX
dis "Objective $\phi$  &  poor students & non-poor students & effect of redistribution to the poor \\"
dis "\hline"
dis "\multicolumn{5}{c}{Assuming only instrument exogeneity}"
forval trow = 1/7 {
	dis "`labl`trow'' " "&  [" %6.3f col1`trow' ", " %6.3f col2`trow' "]  &  [" %6.3f col3`trow' ", " %6.3f col4`trow' "] &  [" %6.3f col5`trow' ", " %6.3f col6`trow' "] &  [" %6.3f col7`trow' ", " %6.3f col8`trow' "]  \\"  
}

dis "\hline"
dis "\multicolumn{5}{c}{Assuming instrument exogeneity and monotonicity}"
forval trow = 8/14 {
	dis "`labl`trow'' " "&  [" %6.3f col1`trow' ", " %6.3f col2`trow' "]  &  [" %6.3f col3`trow' ", " %6.3f col4`trow' "] &  [" %6.3f col5`trow' ", " %6.3f col6`trow' "] &  [" %6.3f col7`trow' ", " %6.3f col8`trow' "]  \\"  
}

*produce output that can be imported into spreadsheets
dis "Now the same for export to CALC"
forval trow = 1/14 {
	dis "`labl`trow'' ;"   %8.3f col1`trow' ";"  %8.3f col2`trow' ";"  %8.3f col3`trow' ";"  %8.3f col4`trow' ";" %8.3f col7`trow' ";"  %8.3f col8`trow'  
}
